package com.sam.im.heiqun.uis.activities.phonecontact;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.sam.im.heiqun.R;
import com.sam.im.heiqun.uis.beans.ContactBean;
import com.sam.im.heiqun.uis.beans.NoticeBean;
import com.yuyh.library.utils.GlideUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by wangguodong on 2017/8/17.
 */

public class PhoneLinkAdapter extends RecyclerView.Adapter<PhoneLinkAdapter.NoticeViewHolder> {

    private Context context ;
    private List<ContactBean> mList ;
    private LayoutInflater inflater;

    public interface OnClickListener{
        void OnClick(ContactBean noticeBean, int type);//0-item点击；1-邀请点击
    }

    private OnClickListener onClickListener;
    public void setOnClickListener(OnClickListener mOnClickListener){
        this.onClickListener = mOnClickListener;
    }

    public PhoneLinkAdapter(Context context, List<ContactBean> mList){
        this.context = context ;
        if (null==mList)mList = new ArrayList<>();
        this.mList = mList ;
        this.inflater = LayoutInflater.from(context);
    }

    @Override
    public NoticeViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.item_phone_user, parent, false);
        NoticeViewHolder viewHolder = new NoticeViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(NoticeViewHolder holder, int position) {
        if (position < mList.size()){

            final ContactBean contactBean = mList.get(position);
            if (null!=contactBean){
                String name = contactBean.getNickName();
                if (null==name || "".equals(name)){
                    name = contactBean.getMarkName();
                }
                holder.recommend_name.setText(name);
                holder.recommend_phone.setText(contactBean.getPhone());

                GlideUtils.loadCircleImage(context,contactBean.getHeader(),holder.head_image);

                if ("1".equals(contactBean.getIsExists())){
                    holder.yaoqing.setVisibility(View.GONE);
                }else {
                    holder.yaoqing.setVisibility(View.VISIBLE);
                }

            }
//
            holder.mview.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onClickListener.OnClick(contactBean,0);
                }
            });

            holder.yaoqing.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onClickListener.OnClick(contactBean,1);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return null==mList?0:mList.size();
    }

    private String getTime(long time){
        SimpleDateFormat format=new SimpleDateFormat("MM-dd");
        Date d1=new Date(time);
        return format.format(d1);
    }

    class NoticeViewHolder extends RecyclerView.ViewHolder {

        ImageView head_image;
        TextView recommend_name;
        TextView recommend_phone;
        Button yaoqing ;

        private View mview ;

        public NoticeViewHolder(View view) {
            super(view);
            mview = view ;
            head_image = (ImageView) view.findViewById(R.id.img);
            recommend_name= (TextView) view.findViewById(R.id.item_name);
            recommend_phone = (TextView) view.findViewById(R.id.item_detail);
            yaoqing = (Button) view.findViewById(R.id.yaoqing);
        }
    }

}
